import { defineStore } from 'pinia'
import { agreeApi, noticeListApi, rejectApi } from '@/api'
import type { NoticeInfo } from '@/types/views/notice'
import { ElMessage } from 'element-plus'
export const useNoticeStore = defineStore('notice', () => {
    const noticeList = ref<NoticeInfo[]>([])
    const unprocessedList = computed(() => {
        return noticeList.value.filter(item => item.status === 0)
    })
    const processedList = computed(() => {
        return noticeList.value.filter(item => item.status !== 0)
    })
    async function getNoticeList() {
        const { data } = await noticeListApi()
        noticeList.value = data
        return 'ok'
    }
    async function agree(reqId: number, id: number) {
        const { data } = await agreeApi(id)
        console.log(data, '同意请求');
        const friendReq = noticeList.value.find(item => item.id === reqId)
        if (friendReq) {
            friendReq.status = 1
        }
        ElMessage.success('已添加')
        return 'ok'
    }
    async function reject(reqId: number, id: number) {
        const { data } = await rejectApi(id)
        console.log(data, '拒绝请求');
        const friendReq = noticeList.value.find(item => item.id === reqId)
        if (friendReq) {
            friendReq.status = 2
        }
        ElMessage.warning('已拒绝')
        return 'ok'
    }
    return { getNoticeList, unprocessedList, processedList, agree, reject }
})
